package com.andaily.domain.log;

import com.andaily.domain.AbstractDomain;
import com.andaily.domain.dto.log.LogDto;
import com.andaily.domain.shared.DateUtils;
import com.andaily.domain.user.User;
import com.andaily.web.context.BeanProvider;
import org.apache.commons.lang.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Shengzhao Li
 */
public class Log extends AbstractDomain {

    protected static LogRepository logRepository() {
        return BeanProvider.getBean(LogRepository.class);
    }


    /**
     * Position: longitude (经度)
     */
    protected BigDecimal longitude;
    /**
     * Position: Latitude (纬度)
     */
    protected BigDecimal latitude;
    /**
     * Log record date
     */
    protected Date logDate;

    /**
     * Last update time
     */
    protected Date lastUpdateTime;
    /**
     * Log content
     */
    protected String content;
    /**
     * The log refer to type
     */
    protected LogType logType;
    /**
     * The log creator
     */
    protected User owner;

    /**
     * Default: public
     */
    protected LogVisibility visibility = LogVisibility.PUBLIC;
    /**
     * Refer a plan, optional.
     */
//    private Plan plan;

    /**
     * Log refers comments
     */
    protected List<LogComment> logComments = new ArrayList<LogComment>();

    public Log() {
    }

    public Log(String content, Date logDate, User owner, LogType logType) {
        this.content = content;
        this.logDate = logDate;
        this.owner = owner;
        this.logType = logType;
    }

    public User owner() {
        return owner;
    }

    public LogType type() {
        return logType;
    }

    public List<LogComment> logComments() {
        return logComments;
    }

    public String content() {
        return content;
    }

    public Date logDate() {
        return logDate;
    }

    public LogVisibility visibility() {
        return visibility;
    }

    public BigDecimal longitude() {
        return longitude;
    }

    public BigDecimal latitude() {
        return latitude;
    }

    public void update(LogDto logDto) {
        this.update(logDto.getContent(), DateUtils.getDate(logDto.getLogDate()), LogVisibility.valueOf(logDto.getVisibility()));

        String typeGuid = logDto.getLogType().getGuid();
        this.logType = logRepository().findLogTypeByGuid(typeGuid);

        String longitude1 = logDto.getLongitude();
        if (StringUtils.isNotEmpty(longitude1)) {
            this.longitude = new BigDecimal(longitude1);
        }

        String latitude1 = logDto.getLatitude();
        if (StringUtils.isNotEmpty(latitude1)) {
            this.latitude = new BigDecimal(latitude1);
        }
        this.lastUpdateTime = DateUtils.now();
    }

    public void update(String content, Date logDate, LogVisibility visibility) {
        this.content = content;
        this.logDate = logDate;
        this.visibility = visibility;
    }

    public void updateOwner(User owner) {
        this.owner = owner;
    }
}
